package com.star.mall.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.star.mall.base.BaseController;
import com.star.mall.persistence.entity.Menu;
import com.star.mall.persistence.service.IMenuService;
import com.star.mall.utils.TreeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 系统菜单表  前端控制器
 * </p>
 *
 * @author StarQ
 * @since 2021-03-14
 */
@RestController
@RequestMapping("/api/v1/menu")
@Api(value = "系统菜单", tags = {"系统菜单"})
public class MenuController extends BaseController<IMenuService, Menu> {

    @GetMapping("/tree")
    @ApiOperation(value = "获取系统菜单树", notes = "获取系统菜单树", httpMethod = "GET")
    public List<Menu> menuTree(@RequestParam(required = false) Integer enable) {
        QueryWrapper<Menu> wrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotEmpty(enable)) {
            wrapper.eq("enable_", enable);
        }
        List<Menu> menus = service.list(wrapper);
        return TreeUtil.listToTree(menus);
    }

    @GetMapping("/tree/{roleId}")
    @ApiOperation(value = "获取某个角色的菜单树", notes = "获取某个角色的菜单树", httpMethod = "GET")
    public List<Menu> roleMenuTree(@PathVariable String roleId) {
        Set<String> roleIds = new HashSet<>();
        roleIds.add(roleId);
        return service.getMenusByRoleIds(roleIds);
    }

    @GetMapping("/current")
    @ApiOperation(value = "当前用户菜单", notes = "当前用户菜单", httpMethod = "GET")
    public List<Menu> currentMenus() {
        return service.getCurrentMenus();
    }

}
